<?php
namespace app\index\controller;


use think\Db;
class TeachingTask extends Common
{

    
    public function index(){
        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }

    public function edit(){
        $id = request()->param('id');
        
        $info = Db('teaching_task')
        ->where('id','=',$id)
        ->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function getTeachingTask(){
        $page = request()->param('page');
        $limit = request()->param('limit');
        $start = ($page-1) * $limit;

        $info = Db('teaching_task')
        ->where('isdelete',0)
        ->limit($start,$limit)
        ->select();
        
        $count =  Db('teaching_task')
        ->limit($start,$limit)
        ->count();

        $msg = [
            'code' => 0,
            'msg' => '查询成功',
            'data' => $info,
            'count' => $count
        ];
        
        echo json_encode($msg);
    }

    public function addPost(){
        $id = Db('teaching_task')->order('id desc')->limit(1)->find()['id'] +1;
        $status=Db('teaching_task')
        ->insert([
            "id" => $id,
            "college" => request()->param("college"), 
            "number_of_professional_teachers" => request()->param("number_of_professional_teachers")
        ]);

        if($status){
            $this->success('教学任务经费添加成功');
        }else{
            $this->error('教学任务经费添加失败');
        }
    }

    public function editPost(){
        $id = request()->param('id');

        $status=Db('teaching_task')
        ->where('id','=',$id)
        ->update([
            "college" => request()->param("college"), 
            "number_of_professional_teachers" => request()->param("number_of_professional_teachers")
        ]);
       
        if($status){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }

    public function remove(){
        if(request()->param('id')){
            $id     = request()->param('id'); //获取id
            $status=Db('teaching_task')
                    ->where('id',$id)
                    ->update([
                        'isdelete' =>"1",
                    ]);
            if($status){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
                exit;
            }
        }
    }

    public function removeselected(){
        //接收参数  
        $map = input();
       
    
        $IDs=implode(',', $map['id']);
        $where = 'id in('.$IDs.')';  

        $list=Db('user')
            ->alias("user")
            ->where($where)
            ->update([
                'isdelete' =>"1",
            ]);
            if($status){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
                exit;
            }

    }
}
